<?php
class QueryString {
  
  var $string = "";
  var $all = array();
  var $any = array();
  var $none = array();
  var $bad = array();
  var $passed = array();
  
  function QueryString(){
    $nargs = func_num_args();
    if($nargs > 0){
      $arg = func_get_arg(0);
      $this->string = $this->CleanString($arg);
    }
  }
  
  function CleanString($str){
    $str = addslashes($str);
  }
  
  function CleanWord($word){
    if(preg_match("[a-zA-Z0-9_", $word)){
      return $word;
    }else{
      $this->passed[] = $word;
      return NULL;
    }
  }
  
  function AddBadWord($word){
    $word = $this->CleanWord($word);
    if(!is_null($word)) $this->bad[] = $word;
  }
  
  function AddBadWords($words){
    if(!is_array($words)){
      $words = explode(" ", $words);
    }
    foreach($words as $word){
      $this->AddBadWord($word);
    }
  }
  
  function AddAnyWord($word){
    $word = $this->CleanWord($word);
    if(!is_null($word)) $this->any[] = $word;
  }
  
  function AddAnyWords($words){
    if(!is_array($words)){
      $words = explode(" ", $words);
    }
    foreach($words as $word){
      $this->AddAnyWord($word);
    }
  }
  
  function AddAllWord($word){
    $word = $this->CleanWord($word);
    if(!is_null($word)) $this->all[] = $word;
  }
  
  function AddAllWords($words){
    if(!is_array($words)){
      $words = explode(" ", $words);
    }
    foreach($words as $word){
      $this->AddAllWord($word);
    }
  }
  
  function AddNoneWord($word){
    $word = $this->CleanWord($word);
    if(!is_null($word)) $this->none[] = $word;
  }
  
  function AddNoneWords($words){
    if(!is_array($words)){
      $words = explode(" ", $words);
    }
    foreach($words as $word){
      $this->AddNoneWord($word);
    }
  }
  
  function ToString(){
    return ($this->string != "") ? $this->string : $this->BuildString();
  }
  
  function BuilString(){
    $values = array(
      "+" => &$this->all,
      "-" => &$this->none,
      " " => &$this->any,
      "~" => &$this->bad);
    $tmp = array();
    foreach($values as $operator => $words)
      if(count($words) > 0)
        $tmp[] = $operator."(".implode(" ", $words).")";
    return implode(" ", $tmp);
  }
}
?> 